package com.ljq.asop;

import java.util.Random;

/**
 * Description:
 * Author:ljq
 * Date:2025-02-16
 * Time:1:09
 */
public class ArrayOperation {
    public static void main(String[] args) {
//        都不能改变
        int[] arr1 = new int[]{1,2,3};  // 静态
        int[] arr2 = {1,2,3,0,5,2,6};
        int[] arr3 = new int[5];  // 动态

        int[][] arr4 = new int[][]{{12,34},{45,78}};  // 静态
        int[][] arr5 = new int[2][3];  // 动态

        shuffleArray(arr2);

        printArray(arr2);
    }


    // 定义一个方法来随机打乱数组
    public static void shuffleArray(int[] array) {
        Random random = new Random();
        for (int i = array.length - 1; i > 0; i--) {
            // 生成一个随机索引，范围是 [0, i]
            int randomIndex = random.nextInt(i + 1);
            // 交换当前元素与随机索引处的元素
            int temp = array[i];
            array[i] = array[randomIndex];
            array[randomIndex] = temp;
        }
    }

    // 辅助方法：打印数组
    public static void printArray(int[] array) {
        for (int num : array) {
            System.out.print(num + " ");
        }
        System.out.println();
    }
}
